// Values from dropdown menus.
var role, state, school, user = 0;

function displayProgressControl(element) {
	$(element).html('<img src="' + IMAGES + 'progress.gif' + '"/>');
}

$(function () {
	$('#role').val('0');
	role = $('#role').val();
	$('#role').change(function (event) {
		event.stopPropagation();
		if ($('#role').val() == '0') {
			$('#role').val(role);
		} else {
			role = $('#role').val();
			$('#state_div').empty();
			$('#school_div').empty();
			$('#user_div').empty();
			$('#signup_div').hide();
			$.ajax({
				type: 'GET',
				url: BASE + 'ajax/states',
				beforeSend: function () {
					displayProgressControl('#state_div');
				},
				success: function (data) {
					$('#state_div').html(data);
					state = $('#state').val();
					$('#state').change(function (event) {
						event.stopPropagation();
						if ($('#state').val() == '0') {
							$('#state').val(state);
						} else {
							state = $('#state').val();
							$('#school_div').empty();
							$('#user_div').empty();
							$('#signup_div').hide();
							$.ajax({
								type: 'GET',
								url: BASE + 'ajax/' + ((role == '1') ? 'colleges/' : 'high_schools/') + state,
								beforeSend: function () {
									displayProgressControl('#school_div');
								},
								success: function (data) {
									$('#school_div').html(data);
									school = $('#school').val();
									$('#school').change(function (event) {
										event.stopPropagation();
										if ($('#school').val() == '0') {
											$('#school').val(school);
										} else {
											school = $('#school').val();
											$('#user_div').empty();
											$('#signup_div').hide();
											$.ajax({
												type: 'GET',
												url: BASE + 'ajax/users/' + role + '/' + school,
												beforeSend: function () {
													displayProgressControl('#user_div');
												},
												success: function (data) {
													$('#user_div').html(data);
													user = $('#user').val();
													$('#user').change(function (event) {
														event.stopPropagation();
														if ($('#user').val() == '0') {
															$('#user').val(user);
														} else {
															user = $('#user').val();
															$('#signup_div').show();
														}
													});
												}
											});
										}
									});
								}
							});
						}
					});
				}
			});
		}
	});
});